﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Text;
using WAD.WEB.AppCode;
using WAD.Model;
using WAD.DAL;

namespace WAD.WEB.Client2.Ajax
{
    /// <summary>
    /// 发财：广告主：扣费记录
    /// </summary>
    public partial class FC_Record : AjaxBase
    {
        public override void AjaxMethodDispatch(String identity)
        {
            switch (identity)
            {
                case "getlist": this.GetList(); break;
                case "get": this.Get(); break;
                case "getclientlist": this.GetClientList(); break;
                case "getrecordlist": this.GetRecordList(); break;
                case "getdetail": this.GetDetail(); break;
            }
        }
        /// <summary>
        /// 广告主：交易记录
        /// </summary>
        [QD.Web.Ajax.AjaxMethod]
        public void GetList()
        {
            IsLogin();
            int user_id = Parse.Int("user_id", 0);
            int direction = Parse.Int("direction", 1);//当前页
            int pageIndex = Parse.Int("page_index", 1);//当前页

            PageData pg = null;
            if (direction == 2)
                pg = DAOFCRecord.GetListPage("id,user_id,amount,balance,direction,articleid,date,title", "id desc", pageIndex, 20, string.Format("user_id={0} and direction={1}", user_id, direction), "v_fc_record_article");
            else
                pg = DAOFCRecord.GetListPage("id,user_id,amount,balance,direction,articleid,date,'' as title", "id desc", pageIndex, 20, string.Format("user_id={0} and direction={1}", user_id, direction), "fc_record");
            if (pg.itemCount == 0)
                DataEmpty();
            Success(pg);
        }

        /// <summary>
        /// 广告主：交易详情
        /// </summary>
        [QD.Web.Ajax.AjaxMethod]
        public void Get()
        {
            IsLogin();
            int user_id = Parse.Int("user_id", 0);
            int id = Parse.Int("id", 0);
            int direction = Parse.Int("direction", 1);//当前页
            if (id < 1) ParamError();

            Dictionary<string, object> model = null;
            if (direction == 2)
                model = DAOFCRecord.Get("v_fc_record_article", "id=" + id);
            else
                model = DAOFCRecord.Get("fc_record", "id=" + id);
            if (model == null) DataEmpty();
            model.Add("ali_name", "");
            model.Add("trade_no", "");  //订单号
            //支付返回的信息
            Dictionary<string, object> order = DAOFCOrder.Get("fc_order", "id=" + model["orderid"], "orderid,platform,callback_msg");

            int platform = Convert.ToInt32(order["platform"]);
            if (platform == 2)
            {
                if (order != null && order["callback_msg"] != null)
                {
                    model["trade_no"] = order["orderid"];
                    Model.JSON.MFCRecord json = JsonParse<Model.JSON.MFCRecord>(Convert.ToString(order["callback_msg"]));
                    if (json != null && json.buyer_email != null)
                        model["ali_name"] = json.buyer_email;
                }
            }
            else if (platform == 1)
            {
                model["trade_no"] = order["orderid"];
                model["ali_name"] = "微信提现";
            }
            Success(model);
        }


        /// <summary>
        /// 返利的下级客户列表
        /// </summary>
        [QD.Web.Ajax.AjaxMethod]
        void GetClientList()
        {
            int uid = Parse.Int("uid", 0);
            int page = Parse.Int("page", 1);
            string nick_name = Parse.String("sitename");

            StringBuilder sb = new StringBuilder();
            sb.AppendFormat(" agentid={0}", UserId);
            if (uid > 0)
                sb.AppendFormat(" and id={0}", uid);

            if (!string.IsNullOrEmpty(nick_name))
                sb.AppendFormat(" and nick_name like '%{0}%'", QD.WX.Utill.Util.FilterSql(nick_name));

            PageData dt = DAO.GetListPage("id,nick_name,phone,state,date", "id desc", page, 20, sb.ToString(), "[user]");
            if (dt.table.Rows.Count == 0)
                DataEmpty();
            JsonResult(0, "", dt);
        }

        #region 返利
        /// <summary>
        /// 返利记录
        /// </summary>
        [QD.Web.Ajax.AjaxMethod]
        void GetRecordList()
        {
            int user_id = Parse.Int("user_id", 0);
            int page = Parse.Int("page", 1);
            int modelid = Parse.Int("modelid", -1);   //2 代理商返利，3运营商返利
            if (user_id < 1)
                ParamError();
            PageData dt = DAO.GetListPage("id,by_user_id,title,op_total,op_price,op_remainder,rebate_state,id as articleid,'' as nick_name,'' as discount", "id desc"
                , page, 20, "user_id=" + user_id, "v_fc_article#user##fanli");
            if (dt.table.Rows.Count == 0)
                DataEmpty();

            //折扣
            Dictionary<string, object> agent = DaoUser.GetDetail(UserId, "nick_name,discount,yunying_discount");
            if (agent != null)
            {
                object discount = agent["discount"];
                if (modelid == 3)
                    discount = agent["yunying_discount"];
                for (int i = 0; i < dt.table.Rows.Count; i++)
                {
                    dt.table.Rows[i]["discount"] = discount;
                    dt.table.Rows[i]["nick_name"] = agent["nick_name"];
                }
            }

            JsonResult(0, "", dt);
        }
        
        /// <summary>
        /// 返利详情
        /// </summary>
        [QD.Web.Ajax.AjaxMethod]
        void GetDetail()
        {
            int id = Parse.Int("id", 0);
            if (id < 1)
                ParamError();
            Dictionary<string, object> dic = DAO.Get("v_fc_article#user##fanli", "id=" + id);
            if (dic == null || dic["id"] == DBNull.Value)
                DataEmpty();

            Dictionary<string, object> agent = DaoUser.GetDetail(UserId, "nick_name,discount,yunying_discount");
            if (agent != null)
            {
                dic.Add("nick_name", agent["nick_name"]);
            }
            Success(dic);
        }

        #endregion
    }
}